import ccxt, datetime, os, time
import pandas as pd
from config.proxy import proxy



def crawl_bybit_datas(symbol, start_time, end_time, limit_count):
    exchange = ccxt.bitfinex(
        {
            'timeout':15000,
            'enableRateLimit':True,
            'proxies': proxy
        }
    )

    current_path = os.getcwd()

    file_dir = os.path.join(current_path, "data/"+symbol.replace('/', ''))

    if not os.path.exists(file_dir):
        os.makedirs(file_dir)

    start_time = datetime.datetime.strptime(start_time, '%Y-%m-%d')
    end_time = datetime.datetime.strptime(end_time, '%Y-%m-%d')
    start_time_stamp = int(time.mktime(start_time.timetuple())) * 1000
    end_time_stamp = int(time.mktime(end_time.timetuple())) * 1000
    while True:
        print("I's starting time")
        print(start_time_stamp, end_time_stamp)
        data = exchange.fetch_ohlcv(symbol, timeframe='1d', since=start_time_stamp, limit=limit_count)

        df = pd.DataFrame(data)
        df.rename(columns={0: 'open_time', 1: 'open', 2: 'high', 3: 'low', 4: 'close', 5: 'volume'}, inplace=True)
        start_time_stamp = int(df.iloc[-1]['open_time'])  # 获取下一个次请求的时间.
        filename = 'data.csv'
        save_file_path = os.path.join(file_dir, filename)
        print("文件保存路径为：%s" % save_file_path)
        # 时间转换成北京时间
        df['open_time'] = pd.to_datetime(df['open_time'], unit='ms')

        df.set_index('open_time', drop=True, inplace=True)
        if not os.path.exists(save_file_path):
            df.to_csv(save_file_path)
        else:
            df.to_csv(save_file_path, mode='a', header=False)

        print("closing to end")
        print(start_time_stamp, end_time_stamp)
        if start_time_stamp > end_time_stamp:
            print("完成数据的请求.")
            break

        time.sleep(0.2)  # 1/25


crawl_bybit_datas('EOS/USDT', '2019-10-15', '2020-11-8', limit_count=500)
